﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager
Public Class EmpleadoDAL : Inherits DBManager

    Public Function NuevoEmpleado(ByVal empleado As Empleado) As String
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("SPInsertEmpleado")

        comando.Parameters.Add("@codigoEmpleado", SqlDbType.Char, 4).Value = empleado.codigoEmpleado
        comando.Parameters.Add("@nombreEmpleado", SqlDbType.VarChar, 30).Value = empleado.nombreEmpleado
        comando.Parameters.Add("@apellidoPaterno", SqlDbType.VarChar, 30).Value = empleado.apellidoPaterno
        comando.Parameters.Add("@apellidoMaterno", SqlDbType.VarChar, 30).Value = empleado.apellidoMaterno
        comando.Parameters.Add("@dniEmpleado", SqlDbType.Char, 8).Value = empleado.dniEmpleado
        comando.Parameters.Add("@emailEmpleado", SqlDbType.VarChar, 60).Value = empleado.emailEmpleado
        comando.Parameters.Add("@telefonoEmpleado", SqlDbType.VarChar, 10).Value = empleado.telefonoEmpleado
        comando.Parameters.Add("@direccionEmpleado", SqlDbType.VarChar, 100).Value = empleado.direccionEmpleado

        comando.Parameters("@codigoEmpleado").Direction = ParameterDirection.InputOutput

        MyBase.ExecuteQuery(comando)

        empleado.codigoEmpleado = comando.Parameters("@codigoEmpleado").Value

        Return empleado.codigoEmpleado

    End Function

    Public Function GetByIdFromEmpleado(ByVal codigoEmpleado As String ) As Empleado 
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetByIdFromEmpleado")
        comando.Parameters.Add("@codigoEmpleado", SqlDbType.Char, 4).Value = codigoEmpleado
        Return SelectObjectFactory(comando).Item(0)
    End Function

    Public Function GetAllFromEmpleado() As List(Of Empleado)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromEmpleado")
        Return SelectObjectFactory(comando)
        
    End Function

    Public Function SearchFromEmpleado(ByVal texto As String) As List(Of Empleado)
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("searchEmpleado")

        comando.Parameters.Add("@texto", SqlDbType.VarChar, 200).Value = texto

        Return SelectObjectFactory(comando)
    End Function

    Public Function DeleteEmpleado(ByVal codigoEmpleado As String) As Boolean
        Dim correcto As Boolean = False
        Dim comando As SqlCommand = MyBase.CreateSelectCommand("DeleteEmpleado")

        comando.Parameters.Add("@codigoEmpleado", SqlDbType.Char, 4).Value = codigoEmpleado

        If MyBase.ExecuteQuery(comando) > 0 Then
            correcto = True
        End If

        Return correcto
    End Function

    Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of Empleado)
        Dim Lista As New List(Of Empleado)
        Using reader As SqlDataReader = MyBase.ExecuteReader(comando)
            While reader.Read
                Dim Empleado As New Empleado(reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetString(5), reader.GetString(6), reader.GetString(7))
                Lista.Add(Empleado)
                Empleado = Nothing
            End While
        End Using
        Return Lista
    End Function


End Class
